/*
 * @(#)LocalCollectionPanel.java
 * 
 * Copyright(C) 2004 Yujin Kim
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.netexplode.jtunes.client.ui.component;

import java.awt.Color;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.List;

import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;

import com.netexplode.jtunes.model.media.MediaCollection;
import com.netexplode.jtunes.model.media.MediaFile;
import com.netexplode.jtunes.model.media.Tag;

/**
 * <code>LocalCollectionPanel</code> represents .....
 * 
 * @author ykim
 * @version $Revision: 1.2 $
 * @since 0.1
 */
public class LocalCollectionPanel extends CollectionPanel {

	private static final long serialVersionUID = 1L;

	public void init() {

		tables = new JTable[collectionManager.getLocalCollections().size()];

		for (int i = 0; i < tables.length; i++) {
			final MediaCollection collection = (MediaCollection) collectionManager.getLocalCollections().get(i);
			final int size = collection.size();
			final List list = collection.getMediaFiles();

			final Object[][] data = new Object[size][columnNames.length];
			for (int j = 0; j < list.size(); j++) {
				final MediaFile mf = (MediaFile) list.get(j);
				Tag tag = mf.getTag();
				if (tag != null) {
					data[j][0] = mf.getTag().getArtist();
					data[j][1] = mf.getTag().getAlbum();
					data[j][2] = mf.getTag().getTitle();

//					try {
//						data[j][0] = new String(mf.getTag().getArtist().getBytes(), "UTF-8");
//						data[j][1] = new String(mf.getTag().getAlbum().getBytes(), "UTF-8");
//						data[j][2] = new String(mf.getTag().getTitle().getBytes(), "EUC-KR");
//					}
//					catch (Exception e) {
//						e.printStackTrace(System.err);
//					}
				}
				collectionLoaded = true;
			}

			tables[i] = new JTable(new DefaultTableModel(data, columnNames) {
				private static final long serialVersionUID = 1L;

				public boolean isCellEditable(int row, int column) {
					return false;
				}
			});

			tables[i].setShowGrid(true);
			tables[i].setGridColor(Color.BLACK);
			tables[i].setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
			// tables[i].setFont(new Font("MacRoman",Font.PLAIN, 12));

			this.addEventLister(tables[i]);
			super.add(getTable());
		}
	}

	protected JTable getTable() {
		return this.tables[collectionManager.getLocalPosition()];
	}
}